package com.icetec.iceintel.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.database.DevicesListSqlHelper;
import com.icetec.iceintel.framework.AppApplication;
import com.icetec.iceintel.framework.IceBaseActivity;
import com.icetec.iceintel.model.Device;
import com.icetec.iceintel.netrequest.InstrutionSender;

/**
 * Created by amo on 2015/3/11.
 * 设置单个灯具额外功能
 */
public class SetlightscaleExtraFunActivity extends IceBaseActivity {
    public static final String ADAPATATION = "adapatation";
    public static final int DELETE_CODE = 0;
    public static final int BACK_CODE = 1;
    TextView backTv;
    TextView titleTv;
    Switch adaptationSwitcher;
    ImageView adaptationTipsIv;
    Intent intent;
    String MAC;
    Device device;
    DevicesListSqlHelper devicesListSqlHelper;
    boolean isAdaptationOn;
    InstrutionSender instrutionSender;
    boolean isDeleted = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setlightscale_config);
        backTv=(TextView)findViewById(R.id.setlightscale_back_tv);
        adaptationSwitcher=(Switch)findViewById(R.id.light_adaptation_switch);
        titleTv=(TextView)findViewById(R.id.setlightscale_name_tv);
        adaptationTipsIv=(ImageView)findViewById(R.id.lightadaption_icon_detail_iv);

        intent = getIntent();
        Bundle bundle=getIntent().getExtras();
        titleTv.setText(bundle.getString("name"));
        adaptationSwitcher.setChecked(bundle.getBoolean(ADAPATATION));
        MAC=bundle.getString("mac");
        devicesListSqlHelper = new DevicesListSqlHelper();
        device = devicesListSqlHelper.getSingleDevice(MAC);
        isAdaptationOn=adaptationSwitcher.isChecked();
        instrutionSender=new InstrutionSender();

        adaptationTipsIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(SetlightscaleExtraFunActivity.this)
                        .setTitle(R.string.light_adaptation)
                        .setIcon(R.drawable.lightadaptation_icon)
                        .setPositiveButton(R.string.i_know,null)
                        .setMessage(R.string.adaptation_explaintation).show();
            }
        });


        backTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        adaptationSwitcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isAdaptationOn=isChecked;
                instrutionSender.setAdapatation(MAC,isChecked);
            }
        });



    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



    public static Intent createIntent(String name,boolean isAdaptationOn,String mac){
        Intent intent=new Intent(AppApplication.getShareInstance(),SetlightscaleExtraFunActivity.class);
        intent.putExtra("name",name);
        intent.putExtra(ADAPATATION,isAdaptationOn);
        intent.putExtra("mac",mac);
        return intent;
    }

    @Override
    public void finish(){
        intent.putExtra(ADAPATATION, isAdaptationOn);
        if(isDeleted){
            setResult(DELETE_CODE, intent);
        }else{
            setResult(BACK_CODE, intent);
        }
        super.finish();
    }

    public void delete(View v){
        Log.i("delete:", "delteclick");
        new AlertDialog.Builder(this)
                .setTitle(R.string.delete_device)
                .setMessage(R.string.is_delete_device)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        devicesListSqlHelper.deledeSingleDevice(device);
                        isDeleted = true;
                        finish();
                    }
                }).setNegativeButton(R.string.cancel, null).show();
    }

}
